package com.education.platform.api.controller.user;

import com.education.platform.api.config.ThreadUser;
import com.education.platform.api.service.SysDictionaryService;
import com.education.platform.api.vo.request.FeedbackQueryVO;
import com.education.platform.api.vo.request.FeedbackUpdateVO;
import com.education.platform.api.vo.response.*;
import com.education.platform.common.enums.DicTypeEnum;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.entity.FeedbackDO;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.entity.SysDictionaryDO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.education.platform.api.service.FeedbackService;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

/**
* describe: 意见反馈
* @author : tomato
* @date: 2020-06-06 02:55:52.718
**/
@RestController
@Api("意见反馈")
@RequestMapping("feedback")
@Validated
public class FeedbackController {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(FeedbackController.class);
    
	@Autowired
    private FeedbackService feedbackService;
    @Autowired
    private SysDictionaryService sysDictionaryService;

    @ApiOperation(value = "获取反馈类型")
    @PostMapping("selectProblemType")
    public List<SysDictionaryResponseVO> selectProblemType() {
        Map<String,List<SysDictionaryResponseVO>> map = new HashMap<>(2);
        List<String> typeCodes = Arrays.asList(DicTypeEnum.PROBLEM_TYPE.getValue());
        List<SysDictionaryDO> dictionaryDTOS = sysDictionaryService.getListByDicTypeCodeList(typeCodes);
        List types =  dictionaryDTOS.parallelStream().filter(m->DicTypeEnum.PROBLEM_TYPE.getValue().equals(m.getTypeCode())).collect(Collectors.toList());
        types = BeanUtil.convert(types,SysDictionaryResponseVO.class);
        return types;
    }


    /**
     * 提交反馈
     * @param feedbackUpdateVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "提交反馈")
    @PostMapping("commitBack")
    public boolean commitBack(@RequestBody @Valid FeedbackUpdateVO feedbackUpdateVO) {
        FeedbackDO dto = BeanUtil.convert(feedbackUpdateVO, FeedbackDO.class);
        dto.setUserId(ThreadUser.getUserId());
        dto.setUserName(ThreadUser.getUserName());
        dto.setCreateTime(new Date());
        if(CollectionUtils.isNotEmpty(feedbackUpdateVO.getPicUrls())){
           String urls = feedbackUpdateVO.getPicUrls().parallelStream().collect(Collectors.joining(","));
            dto.setPicUrl(urls);
        }
        return feedbackService.insert(dto);
    }

    /**
     * 分页查询
     * @param  queryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询意见反馈")
    @PostMapping("getPageList")
    public FeedbackPageInfoVO selectPageList(@RequestBody @Valid FeedbackQueryVO queryVO) {
        FeedbackDO feedbackDO = BeanUtil.convert(queryVO, FeedbackDO.class);

        feedbackDO.setUserId(ThreadUser.getUserId());

        Page<ArticleResponseVO> page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize());
        List<FeedbackDO> articleList = feedbackService.getList(feedbackDO);
        FeedbackPageInfoVO articlePageInfo = new FeedbackPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), articlePageInfo);
        List<FeedbackResponseVO> voList = new ArrayList<>();
        for(FeedbackDO back :articleList){
            FeedbackResponseVO responseVO = BeanUtil.convert(back,FeedbackResponseVO.class);
            if(StringUtils.isNotEmpty(back.getPicUrl())){
                responseVO.setUrls(Arrays.asList(back.getPicUrl().split(",")));
            }
            voList.add(responseVO);
        }
        articlePageInfo.setList(voList);
        return articlePageInfo;
    }

}
